//
//  ProgressView.m
//  CardMoveGame
//
//  Created by peac on 2025/11/11.
//

#import "ProgressView.h"
@interface ProgressView ()

// 背景图层
@property (nonatomic, strong) UIImageView *backgroundImageView;

// 进度条图层
@property (nonatomic, strong) UIImageView *progressImageView;

// 进度条遮罩层
@property (nonatomic, strong) CALayer *maskLayer;

@end

@implementation ProgressView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView {
    // 初始化默认值
    _progress = 0.0f;
    _animationDuration = 0.3f;
    _progressInsets = UIEdgeInsetsMake(2, 2, 2, 2);
    
    // 默认颜色
    _progressColor = [UIColor greenColor];
    _backgroundColor = [UIColor grayColor];
    
    // 创建背景图层
    _backgroundImageView = [[UIImageView alloc] init];
    _backgroundImageView.contentMode = UIViewContentModeScaleToFill;
    [self addSubview:_backgroundImageView];
    
    // 创建进度条图层
    _progressImageView = [[UIImageView alloc] init];
    _progressImageView.contentMode = UIViewContentModeScaleToFill;
    [self addSubview:_progressImageView];
    
    // 创建遮罩层
    _maskLayer = [CALayer layer];
    _maskLayer.backgroundColor = [UIColor whiteColor].CGColor;
    _maskLayer.anchorPoint = CGPointMake(0, 0.5);
    _maskLayer.position = CGPointMake(0, CGRectGetMidY(self.bounds));
    _progressImageView.layer.mask = _maskLayer;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    // 设置背景图层frame
    _backgroundImageView.frame = self.bounds;
    
    // 设置进度条图层frame（考虑边距）
    CGRect progressFrame = UIEdgeInsetsInsetRect(self.bounds, _progressInsets);
    _progressImageView.frame = progressFrame;
    
    // 更新遮罩层frame
    [self updateMaskLayer];
}

#pragma mark - 公开方法

- (void)setProgress:(CGFloat)progress {
    [self setProgress:progress animated:NO];
}

- (void)setProgress:(CGFloat)progress animated:(BOOL)animated {
    // 限制进度值在 0-1 之间
    _progress = MAX(0.0, MIN(1.0, progress));
    
    if (animated) {
        [UIView animateWithDuration:_animationDuration animations:^{
            [self updateMaskLayer];
        }];
    } else {
        [self updateMaskLayer];
    }
}

#pragma mark - 属性设置

- (void)setBackgroundImage:(UIImage *)backgroundImage {
    _backgroundImage = backgroundImage;
    _backgroundImageView.image = backgroundImage;
    
    // 如果有背景图片，清除背景色
    if (backgroundImage) {
        _backgroundImageView.backgroundColor = [UIColor clearColor];
    } else {
        _backgroundImageView.backgroundColor = _backgroundColor;
    }
}

- (void)setProgressImage:(UIImage *)progressImage {
    _progressImage = progressImage;
    _progressImageView.image = progressImage;
    
    // 如果有进度图片，清除进度色
    if (progressImage) {
        _progressImageView.backgroundColor = [UIColor clearColor];
    } else {
        _progressImageView.backgroundColor = _progressColor;
    }
}

- (void)setProgressColor:(UIColor *)progressColor {
    _progressColor = progressColor;
    
    // 如果没有设置进度图片，使用颜色
    if (!_progressImage) {
        _progressImageView.backgroundColor = progressColor;
    }
}

- (void)setBackgroundColor:(UIColor *)backgroundColor {
    _backgroundColor = backgroundColor;
    
    // 如果没有设置背景图片，使用颜色
    if (!_backgroundImage) {
        _backgroundImageView.backgroundColor = backgroundColor;
    }
}

- (void)setProgressInsets:(UIEdgeInsets)progressInsets {
    _progressInsets = progressInsets;
    [self setNeedsLayout];
}

#pragma mark - 私有方法

- (void)updateMaskLayer {
    CGRect progressBounds = _progressImageView.bounds;
    CGFloat maskWidth = progressBounds.size.width * _progress;
    
    // 更新遮罩层的大小和位置
    _maskLayer.bounds = CGRectMake(0, 0, maskWidth, progressBounds.size.height);
    _maskLayer.position = CGPointMake(0, progressBounds.size.height / 2);
}

@end
